package com.lemeng.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import com.lemeng.dao.ParaMapper;
import com.lemeng.dao.SpecMapper;
import com.lemeng.dao.TemplateMapper;
import com.lemeng.entity.PageResult;
import com.lemeng.pojo.Para;
import com.lemeng.pojo.Spec;
import com.lemeng.pojo.Template;
import com.lemeng.service.TemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = TemplateService.class)
public class TemplateServiceImpl implements TemplateService {

    @Autowired
    TemplateMapper templateMapper;

    @Override
    public List<Template> findAll() {
        return templateMapper.selectAll();
    }

    @Override
    public PageResult<Template> findPage(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        PageInfo<Template> templatePageInfo = new PageInfo<>(templateMapper.selectAll());
//        Page<Template> templates = (Page<Template>)templateMapper.selectAll();
        return new PageResult(templatePageInfo.getTotal(),templatePageInfo.getList());
    }

    @Override
    public List<Template> findList(Map<String, Object> serchMap) {
        Example example = new Example(Template.class);
        Example.Criteria criteria = example.createCriteria();
        if (serchMap != null) {
            if (serchMap.get("name")!=null && StringUtil.isNotEmpty(serchMap.get("name").toString())){
                criteria.andLike("name","%"+serchMap.get("name").toString()+"%");
            }
        }
        return templateMapper.selectByExample(example);
    }

    @Override
    public PageResult<Template> findPage(Integer page, Integer size, Map<String, Object> serchMap) {
        PageHelper.startPage(page,size);
        PageInfo<Template> templatePageInfo = new PageInfo<>(findList(serchMap));
        return new PageResult(templatePageInfo.getTotal(),templatePageInfo.getList());
    }

    @Override
    public Template findById(Integer id) {
        return templateMapper.selectByPrimaryKey(id);
    }

    @Override
    public void add(Template template) {
        template.setSpecNum(0);
        template.setParaNum(0);
        templateMapper.insert(template);
    }

    @Override
    public void update(Template template) {
        template.setSpecNum(null);
        template.setParaNum(null);
        templateMapper.updateByPrimaryKeySelective(template);
    }

    @Autowired
    SpecMapper specMapper;

    @Autowired
    ParaMapper paraMapper;

    @Override
    @Transactional
    public void delete(Integer id) {
        //delete from spec where template_id=666
        Spec spec = new Spec();
        spec.setTemplateId(id);
        specMapper.delete(spec);

        Para para = new Para();
        para.setTemplateId(id);
        paraMapper.delete(para);

        templateMapper.deleteByPrimaryKey(id);
    }


}
